﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using NLBS.Data;
using NLBS.Entity;
using System.Web.UI.HtmlControls;
using System.Web;
using NLBS.Framework;
using NLBS.Text;
using NLBS.Web;
using NLBS.Data.Business;

namespace NLBS.Control
{
   public class PanelComment : PanelModule
    {
       [TemplateContainer(typeof(DefaultCommentTemplate))]
        [PersistenceMode(PersistenceMode.InnerProperty)]
        public override ITemplate ItemTemplate
        {
            get
            {
                return base.ItemTemplate;
            }
            set
            {
                base.ItemTemplate = value;
            }
        }

        public override string Title
        {
            get
            {
                return lang["recent_comments"];
            }
            set
            {
                base.Title = value;
            }
        }

        protected override void CreateChildControls()
        {
            if (ItemTemplate == null)
                ItemTemplate = new DefaultCommentTemplate();
            base.CreateChildControls();
        }
    }

    public class DefaultCommentTemplate : ITemplate
    {
        public void InstantiateIn(System.Web.UI.Control container)
        {
            container.Controls.Add(new LiteralControl("<ul>"));

            IList<Comment> commentList =CommentData.GetTopComment(BlogInfo.Instance().RecentCommentList);

            if (commentList != null)
            {
                int j=0;
                for (int i = 0; i < commentList.Count && j < BlogInfo.Instance().RecentArticleList; i++)
                {

                    bool viewPower = ViewHelp.checkCommentViewPermission(commentList[i]);
                    if (viewPower)
                    {
                        j++;
                        Comment comm = commentList[i];
                        if (comm.Content.Length > 10)
                        {
                            comm.Content = comm.Content.Substring(0, 9) + "...";
                        }
                        container.Controls.Add(new LiteralControl("<li>"));
                        string txt = string.Format("<a href=\"{3}\" onclick=\"{0}\" title=\"{2}\">{1}</a>",
                            UrlFormatsProvider.Instance().ArticleConent(comm.LogID),
                            StringHelper.SubStr(15, comm.Content),
                            comm.Content,
                            ReWriteUrls.Instance().ArticleConent(comm.LogID));
                        container.Controls.Add(new LiteralControl(txt));
                        container.Controls.Add(new LiteralControl("</li>"));
                    }
                }
                
            }
            container.Controls.Add(new LiteralControl("</ul>"));
        }
    }
    
}
